<?php

namespace app\admin\logic;

use app\common\exception\LogicException;
use app\model\Barn;

class BarnLogic
{
    /**
     * @Description 列表
     * @author: zj
     * @param array $param
     * @time 2024/03/05 15:21:23
     * @return mixed
     */
    public function getList(array $param)
    {
        $model = new Barn();
        $query = $model->alias('b')
            ->leftJoin('factory f','f.id = b.factory_id')
            ->field('b.id,b.factory_id,f.name as factory,b.name,b.ip,b.feed_base_time,b.record_time,b.create_time')
            ->where('b.delete_time',$model::IS_DELETE_NO);
        if ($param['factory_id']) {
            $query->where('b.factory_id',$param['factory_id']);
        }
        if ($param['name']) {
            $query->where('b.name','like','%'.trim($param['name']).'%');
        }

        return $query->order('b.id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * @Description 详情
     * @author: zj
     * @param int $id
     * @time 2024/03/05 15:29:05
     * @return mixed
     */
    public function detail(int $id)
    {
        $model = new Barn();
        $info = $model->alias('b')
            ->leftJoin('factory f','f.id = b.factory_id')
            ->field('b.id,b.factory_id,f.name as factory,b.name,b.ip,b.feed_base_time,b.record_time,b.create_time')
            ->where('b.id',$id)
            ->where('b.delete_time',$model::IS_DELETE_NO)
            ->find();
        if (!$info) {
            throw new LogicException('数据不存在');
        }

        return $info->toArray();
    }

    /**
     * @Description 下拉选择
     * @author: zj
     * @param array $param
     * @time 2024/03/05 15:39:27
     * @return mixed
     */
    public function selectList(array $param)
    {
        $model = new Barn();
        $query = $model->field('id,name')
            ->where('delete_time',$model::IS_DELETE_NO);
        if ($param['factory_id']) {
            $query->where('factory_id',$param['factory_id']);
        }
        if ($param['name']) {
            $query->where('name','like','%'.trim($param['name']).'%');
        }

        return $query->order('id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

}